package tuvienna.jade;

import jade.wrapper.StaleProxyException;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Meta-Agent who will set up the other agents
 * This will set up the students data.
 * 
 */
public class StudentsSetupAgent extends SetupAgent{
		
			
	private static final long serialVersionUID = 5278005234213008854L;
	
	protected void setup() {
		try {
			String[] eArgs = {"-evaluationIntervall","5000"};
			createNewAgent("Evaluator", "tuvienna.jade.EvaluatorAgent",eArgs);
			
			String[] tArgs  = {"-discussionTickerTime","6000","-gui","false"};
			for (int i=0; i<15; i++) {
				createNewAgent("Table"+i, "tuvienna.jade.table.TableAgent",tArgs);
			}

			// read data from file;
			File file = new File("students.dat");
            FileInputStream is = new FileInputStream(file);
            
            byte[] bytes = new byte[is.available()];
            is.read(bytes); 
            is.close();
            String data = new String(bytes);
            String lines[] = data.split("\r\n");
            String[] interestNames = lines[0].split("\t");
            for (int studentNo=0; studentNo< (lines.length-1); studentNo++) {
            	String[] interestMatrix = lines[studentNo+1].split("\t");
            	String name = interestMatrix[0];
            	List<String> interests = new ArrayList<String>();
            	for (int item=1; item< interestMatrix.length; item++ ) {
            		if (interestMatrix[item].equals("1")) {
            			interests.add(interestNames[item]);
            		}
            	}
            	String[] args  = {"-oldAgeForTable","35","-verbose","false"};
				createNewAgent(	name, "tuvienna.jade.participant.DynamicParticipantAgent",mergeArgs(args,interests.toArray()));
            }
			
			this.doDelete();
			System.out.println("Setup complete");
		} catch (StaleProxyException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
        } catch (java.io.IOException e) {
            e.printStackTrace();
		}	
	}
	

	
	
}
